package com.resifind.www.dao.impl;

import com.resifind.www.dao.ClientDao;
import com.resifind.www.dao.mapper.Mappers;
import com.resifind.www.po.*;
import com.resifind.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 36069
 */
public class ClientDaoImpl implements ClientDao {

    @Override
    public List<Client> queryClientByPage(int pageNum, int pageSize) {
        //language=MySQL
        String sql = "select * from resi_find.client limit ?, ?";
        Object[] params = new Object[]{(pageNum - 1) * pageSize, pageSize};
        return Mappers.clientMapper(sql, params);
    }

    @Override
    public int queryClientCount() {
        //language=MySQL
        String sql = "select count(*) from resi_find.client";
        Object[] params = new Object[]{};
        List<Integer> query = JdbcUtils.query(sql, params, rs -> rs.getInt(1));
        return query.getFirst();
    }

    @Override
    public boolean addClient(int userId, Connection conn) {
        //language=MySQL
        String sql = "insert into resi_find.client (user_id) values (?)";
        Object[] params = {userId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public Client queryClientByClientId(int clientId) {
        //language=MySQL
        String sql = "select * from resi_find.client where id = ?";
        Object[] params = {clientId};
        List<Client> clientList = Mappers.clientMapper(sql, params);
        return clientList.isEmpty() ? null : clientList.getFirst();
    }

    @Override
    public Client queryClientByUserId(int userId) {
        //language=MySQL
        String sql = "select * from resi_find.client where user_id = ?";
        Object[] params = {userId};
        List<Client> clientList = Mappers.clientMapper(sql, params);
        return clientList.isEmpty() ? null : clientList.getFirst();
    }

    @Override
    public boolean updateRealName(Client client, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.client set real_name = ?, ID_num = ? where id = ?";
        Object[] params = {client.getRealName(), client.getIdNum(), client.getId()};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean updatePersonalInfo(Client client, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.client set nickname = ?, age = ?, signature = ? where id = ?";
        Object[] params = {client.getNickname(), client.getAge(), client.getSignature(), client.getId()};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean recharge(int clientId, double amount, Connection conn) {
        //language=MySQL
        String sql = "insert into resi_find.recharge (client_id, amount) values (?, ?)";
        Object[] params = {clientId, amount};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public Recharge queryRechargeByRechargeId(int rechargeId) {
        //language=MySQL
        String sql = "select * from resi_find.recharge where id = ?";
        Object[] params = {rechargeId};
        List<Recharge> rechargeList = Mappers.rechargeMapper(sql, params);
        return rechargeList.isEmpty() ? null : rechargeList.getFirst();
    }

    @Override
    public boolean updateBalance(int clientId, double amount, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.client set balance = balance + ? where id = ?";
        Object[] params = new Object[]{amount, clientId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean updateCoin(int clientId, int bonusCoin, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.client set coin = coin + ? where id = ?";
        Object[] params = new Object[]{bonusCoin, clientId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean updateAvatar(int clientId, String fileName, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.client set avatar = ? where id = ?";
        Object[] params = new Object[]{fileName, clientId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public List<Client> queryAllClients() {
        //language=MySQL
        String sql = "select * from resi_find.client";
        Object[] params = new Object[]{};
        return Mappers.clientMapper(sql, params);
    }

    @Override
    public boolean updateStatus(int clientId, String status, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.client set status = ? where id = ?";
        Object[] params = new Object[]{status, clientId};
        return JdbcUtils.update(sql, params, conn);
    }

}
